package structure;

public class MyQueue1<E> {
    private class Node {
        E val;
        Node next;
        Node prev;

        Node(E val) {
            this.val = val;
        }
    }

    public Node head;
    public Node last;
    private int size;

    public void offer(E val) {
        Node node = new Node(val);
        if (head == null) {
            head = node;
            last = node;
            return;
        }

        last.next = node;
        node.prev = last;
        last = node;
        size++;
    }

    public E poll() {
        if (size == 0) {
            return null;
        }
        E old = head.val;
        head = head.next;
        if (head == null) {
            // 只有一个节点
            last = null;
        }else {
            head.prev.next = null;
            head.prev = null;
        }
        size--;
        return old;
    }


    public E peek() {
        if (size == 0) {
            return null;
        }
        return head.val;
    }

}
